import Configer from "../../../config/Configer";
import {FinalKey} from "../../../../../constant/FinalConstant";
import {ILocalSaveAdapter} from "../../adapterInterface/model1/ILocalSaveAdapter";

export class LocalSaveAdapter implements ILocalSaveAdapter {
    setPath(path: string) {
        log.path = path;
    }
    init() {
        const logPath = Configer.getFinal(FinalKey.logPath)
        const arr = io.open(logPath, "wb");
        if (arr) {
            let wf = arr[0];
            if (wf) {
                wf.write(``)
                wf.close()
            }
        }
    }
    saveLog(str: string) {
        let oldStr = '';
        const logPath = Configer.getFinal(FinalKey.logPath)
        const rfArr = io.open(logPath, "rb")
        if (rfArr) {
            let rf = rfArr[0];
            if (rf) {
                let ciphertext = rf.read("*a" as any) as string;
                // print("ciphertext:" + tostring(ciphertext))
                if (ciphertext) {
                    oldStr = ciphertext;
                }
                rf.close()
            }
        }
        const wfArr = io.open(logPath, "wb");
        if (wfArr) {
            const wf = wfArr[0];
            if (wf) {
                wf.write(`${oldStr}\n${str}`)
                wf.close()
            }
        }
    }
}